package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;
    /**
     * 添加信息到购物车中
     *
     * @param shoppingCartDTO
     */
    public void addShopCart(ShoppingCartDTO shoppingCartDTO) {
        //添加的时候可能有重复添加的，所以需要判断是否有重复的，有重复的就num+1即可，没有就在数据库中新增一条数据
        //查询操作人的购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.selectExit(shoppingCart);

        //当购物车中某个餐的数据不为空时，number+1
        if (list != null && list.size() > 0) {
            ShoppingCart shoppingCart1 = list.get(0);
            Integer number = shoppingCart1.getNumber() + 1;
            shoppingCart.setNumber(number);
            shoppingCartMapper.updateNum(shoppingCart);
            return;
        }
        //当购物车中添加的餐为空时，两种情况
        //添加的是菜品时
        if (shoppingCartDTO.getDishId() != null){
            Dish dish = dishMapper.selectById(shoppingCart.getDishId().intValue());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
            return;
        }
        //添加的是套餐时
        if (shoppingCartDTO.getSetmealId() != null){
            Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId().intValue());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
            return;
        }


    }

    /**
     * 展示购物车
     * @return
     */
    @Override
    public List<ShoppingCart> shopView() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.selectExit(shoppingCart);
        return list;
    }

    /**
     * 情空购物车
     */
    @Override
    public void Clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }
}
